package neves.android.etmg.tilemap;

import java.util.LinkedList;

import android.util.Log;

public class TileMapRoom {

	private int startX;
	private int startY;
	private int roomWidth;
	private int roomHeight;
	private LinkedList<int[]> exitPoints = null;

	public TileMapRoom(int sx, int sy, int rw, int rh) {
		startX = sx;
		startY = sy;
		roomWidth = rw;
		roomHeight = rh;
	}

	public int GetRoomSize() {
		return roomWidth * roomHeight;
	}


	
	
	public boolean IsInRoom(int sx, int sy) {
		return ((sx >= startX && sx < (startX + roomWidth)) && (sy >= startY && sy < (startY + roomHeight)));
	}

	public boolean IsOverlap(int sx, int sy, int rw, int rh) {

		int maxsx = sx + rw + 3;
		int maxsy = sy + rh + 3;

		sx -= 3;
		sy -= 3;

		int roomMaxX = startX + roomWidth;
		int roomMaxY = startY + roomHeight;

		boolean xOverlap = false;
		boolean yOverlap = false;

		// x overlap check
		xOverlap = isLineOverlap(startX, roomMaxX, sx, maxsx);

		// y overlap check
		yOverlap = isLineOverlap(startY, roomMaxY, sy, maxsy);

		// Log.w("ETMG", "= = = =" );

		// Log.w("ETMG",
		// Integer.toString(sx)+"_"+Integer.toString(sy)+"_"+Integer.toString(maxsx)+"_"+Integer.toString(maxsy)
		// );
		// Log.w("ETMG",
		// Integer.toString(startX)+"_"+Integer.toString(startY)+"_"+Integer.toString(roomMaxX)+"_"+Integer.toString(roomMaxY)
		// );
		// Log.w("ETMG", Boolean.toString(xOverlap)+"
		// "+Boolean.toString(yOverlap)+"
		// "+Boolean.toString(yOverlap&&xOverlap));

		return (xOverlap && yOverlap);
	}
	
	// check how many bounder points overlap with input line
	public int[][] BounderOverlapPoints(TileMapRoom lineRoom) {

		int blength = lineRoom.GetRoomSize();
		int[][] overlapPoints = new int[blength][2];

		int pointCount = 0;

		int bsx = startX - 1;
		int bsy = startY - 1;
		int bex = startX + roomWidth ;
		int bey = startY + roomHeight;
		int unity ;

		//Log.w("ETMG", "cr:"+Integer.toString(startX)+","+Integer.toString(startY)+","+Integer.toString(roomWidth)+","+Integer.toString(roomHeight));
		for (int xi = bsx; xi <= bex; xi ++) {
			if((xi==bsx) || (xi == bex)){
				unity = 1;
			}else{
				unity = roomHeight + 1;
			}
			
			for (int yi = bsy; yi <= bey; yi += unity) {
				
				if(((xi==bsx)&&(yi==bsy)) ||
						((xi==bsx)&&(yi==bey)) ||
						((xi==bex)&&(yi==bsy)) ||
						((xi==bex)&&(yi==bey)) 
						){
					
				}else{
				
					if (lineRoom.IsInRoom(xi, yi)) {
						overlapPoints[pointCount++] = new int[] { xi, yi };
					}
				}
				//Log.w("ETMG", "rt:"+Integer.toString(xi)+","+Integer.toString(yi));
			}
		}
		
		if (pointCount > 0) {
			int[][] temPoints = new int[pointCount][2];

			for (int temi = 0; temi < pointCount; temi++) {
				temPoints[temi] = overlapPoints[temi];
			}

			return temPoints;
		} else {
			return new int[0][];
		}
	}
	
	/*
	public int[] yAdjacent(int sx, int sy, int rw, int rh) {

		int[] adjLine = null;
		int maxsx = sx + rw - 1;
		int maxsy = sy + rh - 1;

		// x adjacent
		// if(isLineOverlap(Top(),Bottom()-1,sy,maxsy)){
		if ((sx - (Right() - 1) == 1) || ((Left() - maxsx) == 1)) {

			adjLine = adjacentPoints(Top(), Bottom() - 1, sy, maxsy);

		}
		// }

		return adjLine; // may return null for no adjacent points
	}

	public int[] xAdjacent(int sx, int sy, int rw, int rh) {

		int[] adjLine = null;

		int maxsx = sx + rw - 1;
		int maxsy = sy + rh - 1;

		// if(isLineOverlap(Left(),Right()-1,sx,maxsx)){
		if ((sy - (Bottom() - 1) == 1) || ((Top() - maxsy) == 1)) {
			// return true;

			adjLine = adjacentPoints(Left(), Right() - 1, sx, maxsx);

		}
		// }

		return adjLine; // may return null for no adjacent points
	}
*/
	public void AddExitPoint(int[] exitPoint){
		if(exitPoints == null){
			exitPoints = new LinkedList<int[]>();
		}
		exitPoints.add(exitPoint);
	}
	
	public int[][] GetExitPoint(){
		if(exitPoints==null){
			return new int[0][];
		}else{
			int[][] temPoints = new int[exitPoints.size()][];
			
			for(int temi=0 ; temi<temPoints.length; temi++){
				temPoints[temi] = exitPoints.get(temi);
			}
			
			return temPoints;
		}
		
		
		
	}
	/*
	protected int[] adjacentPoints(int lineAHead, int lineAEnd, int lineBHead,
			int lineBEnd) {
		// (head, end)
		Log.w("ETMG", "jj:" + Integer.toString(lineAHead) + ","
				+ Integer.toString(lineAEnd) + ","
				+ Integer.toString(lineBHead) + ","
				+ Integer.toString(lineBEnd));

		int[] line = new int[2];
		line[0] = lineBEnd + 1;
		line[1] = lineBHead - 1;

		for (int temi = lineBHead; temi <= lineBEnd; temi++) {
			if ((temi >= lineAHead) && (temi <= lineAEnd)) {

				if (temi < line[0])
					line[0] = temi;

				if (temi > line[1])
					line[1] = temi;

			}
		}

		// means no adjacent
		if (line[0] == (lineBEnd + 1)) {
			return null;
		} else {
			line[1]++;
			return line;
		}
	}
*/


	public int Left() {
		return startX;
	}

	public int Right() {
		return startX + roomWidth;
	}

	public int Top() {
		return startY;
	}

	public int Bottom() {
		return startY + roomHeight;
	}
	public int Width(){
		return roomWidth;
	}
	public int Height(){
		return roomHeight;
	}

	protected boolean isLineOverlap(int lineAHead, int lineAEnd, int lineBHead,
			int lineBEnd) {
		// overlap check

		if ((lineAHead < lineBHead) && (lineAEnd < lineBHead)) {
			return false;
		}
		if ((lineAHead > lineBEnd) && (lineAEnd > lineBEnd)) {
			return false;
		}

		return true;
	}

}
